<?php

namespace App\Admin\Controllers;

use App\Models\AdminUser;
use App\Models\DoctorWorks;
use App\Models\WithdrawalLog;
use App\User;
use App\Util\Tool;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class WithdrawalLogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '提现管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WithdrawalLog());
        $grid->disableExport();//禁止导出
        // 去掉筛选工具
        $grid->tools(function (Grid\Tools $tools) {
            $tools->disableFilterButton();
        });
        $grid->disableRowSelector();
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->model()->orderBy('status')->orderByDesc('id');
        $grid->column('id', __('Id'));
        $grid->column('money', __('金额'));
//        $grid->column('user_id', __('User id'));
        $grid->column('type', __('类型'))->using([
            1 => '工作室',
            2 => '用户',
            3 => '供应商',
        ]);
        $grid->column('status', __('状态'))->using([
            0 => '待审核',
            1 => '已提现',
            2 => '已驳回',
        ])->dot([
            0 => 'danger',
            1 => 'success',
            2 => 'success',
        ]);
        // 添加不存在的字段
        $grid->column('company',__('名称'))->display(function () {
            if($this->type == 1){
                return (new  DoctorWorks())->where([['id','=',$this->user_id]])->value("work_name");
            }elseif ($this->type == 2){
                return (new User())->where([['id','=',$this->user_id]])->value("name");
            }elseif ($this->type == 3){
                return (new AdminUser())->where([['id','=',$this->user_id]])->value("name");
            }
        });
        $grid->column('adminuser.name', __('操作人'));
        $grid->column('operation_time', __('操作时间'));
//        $grid->column('sn', __('Sn'));
        $grid->column('created_at', __('申请时间'));
//        $grid->column('updated_at', __('Updated at'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WithdrawalLog::findOrFail($id));
        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
            $tools->disableEdit();
        });
        $show->field('id', __('Id'));
        $show->field('money', __('金额'));
//        $show->field('user_id', __('User id'));
//        $show->field('type', __('Type'));
        $show->field('status', __('提现状态'))->as(function () {
            $option = [
                0 => '待审核',
                1 => '已提现',
                2 => '已驳回'
            ];
            return $option[$this->status] ?? '';
        });

//        $show->field('operation_uid', __('Operation uid'));
//        $show->field('operation_time', __('Operation time'));
//        $show->field('sn', __('Sn'));
        $show->field('created_at', __('申请时间'));
//        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WithdrawalLog());
        $form->decimal('money', __('提现金额'))->disable();
        $form->radio('status', __('审核状态'))->options([1 => "已提现", 2 => "驳回"])->default(0)->rules('required');
        $form->textarea('back_reason', __('驳回原因'));
        $userid = auth()->user()->id ?? 0;
//        $form->hidden('_token')->default(csrf_token());
        $form->hidden("operation_uid",__('操作人'))->default($userid);
        $form->hidden("operation_time",__('操作时间'))->default(date("Y-m-d H:i:s",time()));
        $form->saving(function ($form) {
            $status  = $form->model()->status;
            if($status != 0){
                    $error = new MessageBag([
                        'title' => '温馨提示',
                        'message' => '该记录已被审核,不能重复提交',
                    ]);
                    return back()->with(compact('error'));
            }
            if ($form->status == 0) {
                $error = new MessageBag([
                    'title' => '温馨提示',
                    'message' => '请选择审核状态',
                ]);
                return back()->with(compact('error'));
            }
            if ($form->status == 1) {
                $error = new MessageBag([
                    'title' => '温馨提示',
                    'message' => '暂未开通该功能!',
                ]);
                return back()->with(compact('error'));
                //提现操作
            }
            if ($form->status == 2) {
                if(empty($form->back_reason)){
                    $error = new MessageBag([
                        'title' => '温馨提示',
                        'message' => '驳回原因不能为空!',
                    ]);
                    return back()->with(compact('error'));
                }
            }
        });
        $form->saved(function ($form) {
            $res = (new WithdrawalLog())->where([['id', '=', $form->model()->id]])->first();
            if ($form->status == 2) {
                $type = $res->type;
                if ($type == 1) {  //工作室
                    (new DoctorWorks())->where([['id', '=', $res->user_id]])->increment("surplus", $res->money);
                } elseif ($type == 2) { //用户
                    return (new User())->where([['id', '=', $res->user_id]])->increment("surplus", $res->money);
                } elseif ($type == 3) { //供应商
                    (new AdminUser())->where([['id', '=', $res->user_id]])->increment("surplus", $res->money);
                }
            }
        });
//        $form->number('status', __('Status'));
//        $form->number('operation_uid', __('Operation uid'));
//        $form->datetime('operation_time', __('Operation time'))->default(date('Y-m-d H:i:s'));
//        $form->text('sn', __('Sn'));

        return $form;
    }
}
